package com.tzh.generator;

import com.tzh.plugin.base.CommentPlugin;
import com.tzh.plugin.base.LombockPlugin;
import com.tzh.plugin.base.MapperGeneratorPlugin;
import com.tzh.plugin.base.SwaggerPlugin;
import com.tzh.plugin.base.XMLGeneratorPlugin;
import com.tzh.plugin.ex.ImplServiceGeneratorPlugin;
import com.tzh.plugin.ex.RestGeneratePlguin;
import com.tzh.plugin.ex.ServiceGeneratorPlugin;
import com.tzh.proxy.ConfigurationCGlibProxy;
import com.tzh.proxy.ContextCGlibProxy;
import com.tzh.utils.PropertiesUtils;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.CommentGeneratorConfiguration;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.Context;
import org.mybatis.generator.config.JDBCConnectionConfiguration;
import org.mybatis.generator.config.PluginConfiguration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;
import org.mybatis.generator.plugins.EqualsHashCodePlugin;
import org.mybatis.generator.plugins.SerializablePlugin;
import org.mybatis.generator.plugins.ToStringPlugin;

import java.io.File;
import java.util.ArrayList;
import java.util.List;


public class OracleGBR {

    static void generate() {
        try {
            List<String> warnings = new ArrayList<String>();
            final boolean overwrite = true;
            File configFile = new File("src/main/resources/generatorConfig_oracle.xml");
            ConfigurationParser cp = new ConfigurationParser(warnings);
            ConfigurationCGlibProxy proxy = new ConfigurationCGlibProxy();
            Configuration c = cp.parseConfiguration(configFile);
            Configuration config = proxy.CreateContextProxyObject(c);//
            //生成Context的代理对象
            ContextCGlibProxy cGlibProxy = new ContextCGlibProxy();
            Context context = null;
            if (config.getContext("Oracle") != null) {
                context = cGlibProxy.CreateContextProxyObject(config.getContext("Oracle"));
                JDBCConnectionConfiguration conn = context.getJdbcConnectionConfiguration();
//				conn.setConnectionURL("jdbc:oracle:thin:@192.168.100.25:1521/ZXIN");
                conn.setDriverClass("oracle.jdbc.driver.OracleDriver");
            } else if (config.getContext("Mysql") != null) {
                context = cGlibProxy.CreateContextProxyObject(config.getContext("Mysql"));
                JDBCConnectionConfiguration conn = context.getJdbcConnectionConfiguration();
                conn.setDriverClass("com.mysql.cj.jdbc.Driver");
            }
//			Context context = config.getContext("Oracle");
            context.setCommentGeneratorConfiguration(generateCommentGeneratorConfiguration());
            config.addContext(context);
            List<Context> contexts = config.getContexts();
            //移除之前的context对象，使用context代理对象
            contexts.remove(0);
            setPlugins(context);
            DefaultShellCallback callback = new DefaultShellCallback(overwrite);
            MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
            myBatisGenerator.generate(null);
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
    }

    private static void setPlugins(Context context) {
        PluginConfiguration swaggerPliConfiguration = getConfiguration(SwaggerPlugin.class.getTypeName());
        context.addPluginConfiguration(swaggerPliConfiguration);
        if (Boolean.valueOf(PropertiesUtils.getConfigValue("lombok.enabled", "false"))) {
            PluginConfiguration lombockPluginConfiguration = getConfiguration(LombockPlugin.class.getTypeName());
            context.addPluginConfiguration(lombockPluginConfiguration);
            context.addPluginConfiguration(getConfiguration(SerializablePlugin.class.getTypeName()));
        } else {
            PluginConfiguration commentPluginConfiguration = getConfiguration(CommentPlugin.class.getTypeName());
            context.addPluginConfiguration(commentPluginConfiguration);
            context.addPluginConfiguration(getConfiguration(SerializablePlugin.class.getTypeName()));
            context.addPluginConfiguration(getConfiguration(EqualsHashCodePlugin.class.getTypeName()));
            context.addPluginConfiguration(getConfiguration(ToStringPlugin.class.getTypeName()));
        }
        PluginConfiguration mapperPluginConfiguration = getConfiguration(MapperGeneratorPlugin.class.getTypeName());
        context.addPluginConfiguration(mapperPluginConfiguration);
        PluginConfiguration xmlPluginConfiguration = getConfiguration(XMLGeneratorPlugin.class.getTypeName());
        xmlPluginConfiguration.addProperty("suppressTypeWarnings", "true");
        xmlPluginConfiguration.addProperty("suppressAllComments", "true");
        xmlPluginConfiguration.addProperty("suppressDate", "true");
        context.addPluginConfiguration(xmlPluginConfiguration);
        //业务相关代码
        PluginConfiguration servicePluginConfiguration = getConfiguration(ServiceGeneratorPlugin.class.getTypeName());
        context.addPluginConfiguration(servicePluginConfiguration);
        PluginConfiguration serviceImplPluginConfiguration = getConfiguration(ImplServiceGeneratorPlugin.class.getTypeName());
        context.addPluginConfiguration(serviceImplPluginConfiguration);
        PluginConfiguration restPluginConfiguration = getConfiguration(RestGeneratePlguin.class.getTypeName());
        context.addPluginConfiguration(restPluginConfiguration);

    }


    public static void main(String[] args) {
        System.out.println("0000");
        generate();
    }

    private static PluginConfiguration getConfiguration(String className) {
        PluginConfiguration pliConfiguration = new PluginConfiguration();
        pliConfiguration.setConfigurationType(className);
        return pliConfiguration;
    }

    private static CommentGeneratorConfiguration generateCommentGeneratorConfiguration() {
        CommentGeneratorConfiguration commentGeneratorConfiguration = new CommentGeneratorConfiguration();
        commentGeneratorConfiguration.addProperty("wocao ", "true");
        return commentGeneratorConfiguration;

    }

}
